<template>
  <div :id="id"></div>
</template>

<script>
export default {
  name: "cu-gauge",
  props: {
    id: {
      type: String,
      required: true,
    },
    progress: {
      type: Number,
      default: 100,
    },
    chartColor: {
      type: String,
      default: "blue",
    },
    borderWith: {
      type: Number,
      default: 1,
    },
  },
  data() {
    return {
      chartRef: null,
    };
  },
  methods: {
    initChart() {
      this.chartRef = this.$echarts.init(document.getElementById(this.id));
    },
    setChart() {
      let option = {
        series: [
          {
            backgroundColor: "#ccc",
            type: "gauge",
            startAngle: 90 + Math.floor((this.progress * 9) / 5),
            endAngle: 90 - Math.floor((this.progress * 9) / 5),
            radius: "80%",
            axisLine: { show: false },
            pointer: { show: false },
            axisTick: { show: false },
            splitLine: { show: false },
            axisLabel: { show: false },
            title: { show: false },
            detail: {
              fontSize: 12,
              color: this.chartColor,
              offsetCenter: [0, "0%"],
              valueAnimation: true,
              formatter: (value) => value + "%",
            },
            progress: {
              show: true,
              width: true,
              itemStyle: {
                color: "#fff",
                borderWidth: this.borderWidth,
                borderColor: this.chartColor,
                borderType: [1, 2],
                borderDashOffset: 1,
              },
            },
            data: [
              {
                value: this.progress,
              },
            ],
          },
        ],
      };
      this.chartRef.setOption(option);
    },
  },
  mounted() {
    this.initChart();
    this.setChart();
  },
};
</script>

<style scoped></style>
